import { useHttp } from '../http'
const PROXY_AUTH_API = 'api'
const PROXY_AUTH_LOGIN = 'request'
/**
 * 发送短信验证码信息
 */
export const sendLoginSms = mobile => {
  return useHttp.get(
    `${PROXY_AUTH_API}/code/sms`,
    { mobile },
    { hideLogin: true }
  )
}

/**
 * 授权登录处理，支持用户名/密码|短信验证码|二维码等
 */
export const loginIn = (data, client) => {
  // 登录必须为form表单，由于是oatuh2.0支持指定模式
  const formData = new FormData()
  Object.keys(data).forEach(key => {
    formData.append(key, data[key])
  })

  // 组装特定的参数信息
  const headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    Authorization: 'Basic ' + client
  }

  return useHttp.post(
    `${PROXY_AUTH_LOGIN}/oauth2/token`,
    formData,
    {headers: headers}
  )
}

/**
 * 用户退出登录操作
 */
export const loginOut = () => {
  return useHttp.post(`${PROXY_AUTH_API}/login/out`)
}

/**
 * 刷新token
 */
export const refreshTokenApi = (refreshToken, client) => {
  const data = { refresh_token: refreshToken, grant_type: 'refresh_token' }
  const formData = new FormData()
  Object.keys(data).forEach(key => {
    formData.append(key, data[key])
  })

  // 组装特定的参数信息
  const headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    Authorization: 'Basic ' + client
  }

  return useHttp.post(
    `${PROXY_AUTH_LOGIN}/oauth2/token`,
    formData,
    {headers: headers, hideLoading: true}
  )
}

/**
 * 用户强制登陆
 */
export const forceLogin = data => {
  return useHttp.post(`${PROXY_AUTH_API}/login/force`, data)
}

export const testApi = (data) => {
  return useHttp.get(`api/login/force`, data)
}
